package com.lemon.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.boot.common.base.BaseController;
import com.lemon.boot.common.web.Result;
import com.lemon.boot.dto.ResourceDTO;
import com.lemon.boot.po.ResourcePO;
import com.lemon.boot.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 资源接口方法控制层
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Api(tags = "资源管理")
@RestController
@RequestMapping("resource")
@PreAuthorize("hasRole('ROLE_SUPER')")
public class ResourceController {
    @Resource
    private IResourceService iResourceService;

    /**
     * 添加单条数据
     *
     * @param resourcePO
     * @return
     */
    @ApiOperation(value = "添加单条数据")
    @PostMapping("add")
    public String add(@RequestBody ResourcePO resourcePO) {
        return iResourceService.add(resourcePO);
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除数据")
    @DeleteMapping("del/{id}")
    public String del(@PathVariable("id") Long id) {
        return iResourceService.del(id);
    }

    /**
     * 根据ID修改数据（空字段不更新）
     *
     * @param resourcePO
     * @return
     */
    @ApiOperation(value = "根据ID修改数据（空字段不更新）")
    @PutMapping("edit")
    public String edit(@RequestBody ResourcePO resourcePO) {
        return iResourceService.edit(resourcePO);
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询数据")
    @GetMapping("get/{id}")
    public ResourcePO get(@PathVariable("id") Long id) {
        return iResourceService.get(id);
    }

    /**
     * 根据实体类查询分页数据
     *
     * @param resourceDTO
     * @return
     */
    @ApiOperation(value = "根据实体类查询分页数据")
    @GetMapping("page")
    public Page<?> page(ResourceDTO resourceDTO) {
        return iResourceService.page(resourceDTO);
    }

    /**
     * 启用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "启用")
    @PutMapping("enable/{id}")
    public String enable(@PathVariable("id") Long id) {
        return iResourceService.enable(Collections.singletonList(id));
    }

    /**
     * 停用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "停用")
    @PutMapping("unable/{id}")
    public String unable(@PathVariable("id") Long id) {
        return iResourceService.unable(Collections.singletonList(id));
    }
}